//
//  personAlproFile.m
//  HappyStore
//
//  Created by 李小辉 on 16/6/25.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "personAlproFile.h"

@interface personAlproFile ()<UITextViewDelegate>

@property (strong, nonatomic) UITextView *personAlproFileTextView;
@property (strong, nonatomic) UILabel *tipLabel;
@property (strong, nonatomic) UILabel *countLabel;
@end

@implementation personAlproFile

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBarController.tabBar.hidden = YES;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"个人简介";
    titleLabel.textColor = RGB(45, 45, 45);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(btnClickRight)];
    rightBar.tintColor = RGB(45, 45, 45);
    self.navigationItem.rightBarButtonItem = rightBar;
    
    //解决导航栏挡住下面的类容
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
   
    
    //创建文本框
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, screenW- 20, screenH - 200)];
    textView.text = @"";
    textView.font = [UIFont fontWithName:@"Arial" size:12.0f];
    textView.backgroundColor = [UIColor whiteColor];
    textView.textAlignment = NSTextAlignmentLeft;
    textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textView.returnKeyType = UIReturnKeyDefault;
    textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    textView.layer.borderWidth = 1.0;
    textView.layer.cornerRadius = 8.0f;
    textView.layer.masksToBounds = YES;
    textView.scrollEnabled = NO;
    textView.delegate = self;
    self.personAlproFileTextView = textView;
    //自定义文本框
    UILabel * tip = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 20, 25)];
    if ([self.alproFileStr isEqualToString:@""]||[self.alproFileStr isEqualToString:@"null"]||self.alproFileStr == nil) {
        tip.text = @"请输入需求详情";
    }else{
        self.personAlproFileTextView.text = self.alproFileStr;
    }
    tip.font = [UIFont fontWithName:@"Arial" size:12.0f];
    tip.backgroundColor = [UIColor clearColor];
    tip.enabled = NO;
    self.tipLabel = tip;
    
    /*自定义文本框字数统计*/
    UILabel * count = [[UILabel alloc]initWithFrame:CGRectMake(screenW - 90,self.personAlproFileTextView.height - 30, 80, 30)];
    count.text = @"0/500";
    count.textAlignment = NSTextAlignmentCenter;
    count.font = [UIFont fontWithName:@"Arial" size:12.0f];
    count.backgroundColor = [UIColor clearColor];
    count.enabled = NO;
    self.countLabel = count;
    
    [self.view addSubview:self.personAlproFileTextView];
    [self.personAlproFileTextView addSubview:self.tipLabel];
    [self.personAlproFileTextView addSubview:self.countLabel];
    
    
    
    
}


//进行字数限制
- (void)textViewDidChange:(UITextView *)textView
{
    
    textView = self.personAlproFileTextView;
    NSInteger number = [textView.text length];
    if (number >= 500) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"字符个数不能大于500" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        textView.text = [textView.text substringToIndex:200];
        number = 200;
        
    }
    self.countLabel.text = [NSString stringWithFormat:@"%ld/500",(long)number];
    
    if (textView.text.length > 0) {
        self.tipLabel.text = @"";
        
    }else{
        self.tipLabel.text = @"输入个人详情";
    }
}



//返回
-(void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
//完成
-(void)btnClickRight{
    NSMutableDictionary  *dic = [NSMutableDictionary dictionary];
    [dic setObject:self.personAlproFileTextView.text forKey:@"string"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"personAlproFileTextViewText" object:nil userInfo:dic];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
